#!/usr/bin/python

import os
import sys
import bsddb
from itertools import groupby

def main():
    output_dir = sys.argv[1]
    assert os.path.exists(output_dir)

    cnt_all = 0
    cnt_genomes = 0
    for genome_id, grp in groupby((line.split() for line in sys.stdin), key=lambda spl: spl[1]):
        cnt_genomes += 1
        f = open(os.path.join(output_dir, genome_id), 'w')
        f.write("%s\n" % (genome_id,))
        for spl in grp:
            cnt_all += 1
            f.write( "%s\t%s\t%s\t%s\t%s\n" % (spl[0], spl[6], spl[7], spl[8], spl[9]) )
        f.close()
    
    print "Readed %s genomes, %s genes" % (cnt_genomes, cnt_all)


if __name__ == '__main__':
    main()

